package com.yutel.apk.tool.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	public static String createMd5(File file) {
		MessageDigest mMDigest;
		FileInputStream Input;
		byte buffer[] = new byte[1024];
		int len;
		if (!file.exists()) {
			return null;
		}
		try {
			mMDigest = MessageDigest.getInstance("MD5");
			Input = new FileInputStream(file);
			while ((len = Input.read(buffer, 0, 1024)) != -1) {
				mMDigest.update(buffer, 0, len);
			}
			Input.close();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		BigInteger mBInteger = new BigInteger(1, mMDigest.digest());
		return mBInteger.toString(16);

	}

	public static boolean checkMd5(String Md5, File file) {
		if (Md5 == null) {
			return false;
		}
		String str = createMd5(file);
		System.out.println("md5sum=" + str);
		if (Md5.compareTo(str) == 0) {
			return true;
		} else {
			return false;
		}
	}
}
